package com.eskie.vmobileapp;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;

import com.actionbarsherlock.app.SherlockActivity;
import com.actionbarsherlock.view.Menu;
import com.actionbarsherlock.view.MenuItem;
import com.actionbarsherlock.view.MenuItem.OnMenuItemClickListener;

public class HomeActivity extends SherlockActivity {

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		// Get the view from activity_main.xml
		setContentView(R.layout.main);
		getSupportActionBar().setIcon(R.drawable.vmobile);
		getSupportActionBar().setDisplayShowTitleEnabled(false);
		LayoutInflater inflater = LayoutInflater.from(this);
		View customView = inflater.inflate(R.layout.title_home, null);
		getSupportActionBar().setCustomView(customView);
		getSupportActionBar().setDisplayShowCustomEnabled(true);

		// Accessing the Internal Storage of the Device
		SharedPreferences pref = getSharedPreferences(CustomDialog.conf_pref, 0);

		// Getting the values inside the storage
		GlobalVar.ID = pref.getString("saveID", "");
		GlobalVar.Gateway = pref.getString("saveGate", "");
	}
	
	@Override
	public void onResume(){
		super.onResume();
		if (GlobalVar.ID == "" || GlobalVar.Gateway== "") {
			AlertDialog.Builder builder = new AlertDialog.Builder(this);
			builder.setMessage("Config your account now!")
					.setTitle("Configuration").setCancelable(false);
			builder.setPositiveButton("Config now!",
					new DialogInterface.OnClickListener() {
						public void onClick(DialogInterface dialog, int which) {
							// Switch Activity to Configuration Menu
							Intent intent = new Intent(HomeActivity.this,
									ConfigActivity.class);
							startActivity(intent);
						}
					});
			builder.setCancelable(false);
			AlertDialog dialog = builder.create();
			dialog.show();
		}
	}

	public void onButtonClicker(View v) {
		Intent intent;

		switch (v.getId()) {

		case R.id.main_btn_balance:
			intent = new Intent(this, BalanceActivity.class);
			startActivity(intent);
			break;

		case R.id.main_btn_transfer:
			intent = new Intent(this, TransferActivity.class);
			startActivity(intent);
			break;

		case R.id.main_btn_reload:
			intent = new Intent(this, ReloadActivity.class);
			startActivity(intent);
			break;

		case R.id.main_btn_load:
			intent = new Intent(this, LoadActivity.class);
			startActivity(intent);
			break;
		default:
			break;
		}
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {

		// Third Menu Button
		menu.add("Configure")
				.setOnMenuItemClickListener(this.ConfigButtonListener)
				.setIcon(R.drawable.settings) // Set the menu icon
				.setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM);

		return super.onCreateOptionsMenu(menu);
	}

	// Capture third menu button click
	OnMenuItemClickListener ConfigButtonListener = new OnMenuItemClickListener() {

		public boolean onMenuItemClick(MenuItem item) {
			Intent intent = new Intent(HomeActivity.this, ConfigActivity.class);
			startActivity(intent);
			return false;
		}
	};
}
